//
//  logViewController.swift
//  V2ex-Swift
//
//  Created by Jasper on 2/28/16.
//  Copyright © 2016 Fin. All rights reserved.
//

import UIKit
import YYKit
import SVProgressHUD
import AVOSCloud

class LoginViewController: RootViewController, UITextFieldDelegate {
   
    @IBOutlet weak var line1Height: NSLayoutConstraint!
    @IBOutlet weak var line2Height: NSLayoutConstraint!
    @IBOutlet weak var loginButton: UIButton!
    
    @IBOutlet weak var loginNameTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "登陆高楼迷"
        
        line1Height.constant = 0.5
        line2Height.constant = 0.5
        
        loginButton.layer.cornerRadius = 3
        loginButton.layer.masksToBounds = true
        loginButton.setBackgroundImage(UIImage.init(color: UIColor.mainColor()), for: UIControlState())
    }

    @IBAction func buttonClicked(_ sender: UIButton) {
        
        MineObjectHelper.login(with: sender.tag) { (success: Bool) in
            if success {
                self.navigationController?.popViewController(animated: true)
            } else {
                SVProgressHUD.showError(withStatus: "授权失败")
            }
            print(success)
        }
    }
    
    @IBAction func loginButtonClicked(_ sender: AnyObject) {
       login()
    }
    
    func login() {
        if loginNameTextField.text == nil || loginNameTextField.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入用户名")
            return
        } else if passwordTextField.text == nil || passwordTextField.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入密码")
            return
        }
        
        SVProgressHUD.show(withStatus: "登录中...")
        AVUser.logInWithUsername(inBackground: loginNameTextField.text!, password: passwordTextField.text!) { (user: AVUser?, error: Error?) in
            if error != nil {
                print("登录失败")
                SVProgressHUD.showError(withStatus: error?.localizedDescription)
                print(error)
            } else {
                print("登录成功")
                AVAnalytics.event("登录成功")
                SVProgressHUD.showSuccess(withStatus: "登录成功")
                print(user)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    
    @IBAction func tapGesture(_ sender: UITapGestureRecognizer) {
        loginNameTextField.resignFirstResponder()
        passwordTextField.resignFirstResponder()
    }
    func alertError(_ error: NSError?) -> Bool {
        if error != nil {
            if error?.domain == NSURLErrorDomain  {
                print("网络连接发生错误")
            } else if error!.code == Int(AVOSCloudSNSErrorCode.userCancel.rawValue) {
                print("取消了登陆")
            } else {
                #if DEBUG
                    print(error?.localizedDescription)
                #endif
            }
            return true
        } else {
            return false
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.tag == 0 { // 用户名
            passwordTextField.becomeFirstResponder()
        } else if textField.tag == 1 { // 密码
            login()
        }
        return true
    }
    
}
